<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>新增菜单</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css">
    <link rel="stylesheet" href="../../../admin/css/dimples.css">
    <link rel="icon" href="../../../admin/images/favicon.ico" type="image/x-icon"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">父级菜单</label>
                    <div class="layui-input-block">
                        <ul id="selectParent" name="parentId" class="dtree" data-id="-1"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">菜单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="menuName" lay-verify="title" required autocomplete="off"
                               placeholder="菜单名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="permsItem">
                    <label class="layui-form-label">权限标识</label>
                    <div class="layui-input-block">
                        <input type="text" id="perms" name="perms" autocomplete="off" placeholder="权限标识"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单状态</label>
                    <div class="layui-input-block">
                        <input type="radio" lay-filter="type" name="type" value="0" title="目录">
                        <input type="radio" lay-filter="type" name="type" value="1" title="菜单" checked>
                        <input type="radio" lay-filter="type" name="type" value="2" title="按钮">
                    </div>
                </div>
                <div class="layui-form-item" id="menuUrlItem">
                    <label class="layui-form-label">菜单路径</label>
                    <div class="layui-input-block">
                        <input type="text" id="menuUrl" name="menuUrl" autocomplete="off" placeholder="菜单路径"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="openTypeItem">
                    <label class="layui-form-label">打开方式</label>
                    <div class="layui-input-block">
                        <select name="openType" id="openType">
                            <option value=""></option>
                            <option value="_iframe">框架</option>
                            <option value="_blank">签页</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单图标</label>
                    <div class="layui-input-block">
                        <input type="text" id="icon" name="icon" lay-filter="icon" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="orderNum" lay-verify="title" autocomplete="off" placeholder="排序"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="menu-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<!-- 依 赖 脚 本 -->
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../component/pear/dimples.js"></script>
<script>
    layui.use(['form', 'jquery', 'iconPicker', 'dictionary', 'xmSelect', 'ajax', 'loading'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let iconPicker = layui.iconPicker;
        let xmSelect = layui.xmSelect;
        let ajax = layui.ajax;
        let loading = layui.loading;

        let menuXmlSelect;

        ajax.send('/system/menu/tree', {
            data: {
                virtualNode: true
            }
        }, function (r) {
            let d = r.data;
            menuXmlSelect.update({
                data: d
            });
            menuXmlSelect.setValue([0]);
        });

        menuXmlSelect = xmSelect.render({
            el: '#selectParent',
            model: {label: {type: 'text'}},
            clickClose: true,
            tree: {
                show: true,
                strict: false,
                showLine: false,
                clickCheck: true,
                expandedKeys: [-1],
            },
            name: 'parentId',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'id',
                name: 'title',
                children: 'children'
            },
            height: 'auto',
            on: function (data) {
                if (data.isAdd) {
                    return data.change.slice(0, 1)
                }
            }
        });

        form.on("radio(type)", function () {
            if (this.value == '0') {
                $("#menuUrlItem").hide();
                $("#permsItem").hide();
                $("#openTypeItem").hide();
                $("#menuUrl").val("");
                $("#perms").val("");
                $("#openType").val("");
            } else if (this.value == '1') {
                $("#menuUrlItem").show();
                $("#permsItem").show();
                $("#openTypeItem").show();
            } else if (this.value == '2') {
                $("#menuUrlItem").hide();
                $("#openTypeItem").hide();
                $("#permsItem").show();
                $("#menuUrl").val("");
                $("#openType").val("");
            }
        });

        form.on('submit(menu-save)', function (data) {
            loading.Load('1', '')
            data.field.icon = "layui-icon " + data.field.icon;
            if (data.field.parentId || data.field.parentId == "") {
                data.field.parentId = menuXmlSelect.getValue()[0].id;
            }
            ajax.send('/system/menu', {
                type: 'POST',
                data: JSON.stringify(data.field),
            }, function (result) {
                layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                    parent.render();
                });
            });
            return false;
        });

        iconPicker.render({
            // 选择器，推荐使用input
            elem: '#icon',
            // 数据类型：fontClass/unicode，推荐使用fontClass
            type: 'fontClass',
            // 是否开启搜索：true/false
            search: true,
            // 是否开启分页
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {
                console.log(data);
            }
        });
    })
</script>
<script>
</script>
</body>
</html>